/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.rest;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mcx.modules.domain.Account;
import com.mcx.modules.service.AccountService;
import com.mcx.modules.service.dto.AccountQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
* @website https://el-admin.vip
* @author mcx
* @date 2021-10-21
**/
@Controller
@RequiredArgsConstructor
@RequestMapping("/api/account")
public class AccountController {

    private final AccountService accountService;

    @GetMapping
    public ResponseEntity<Object> query(AccountQueryCriteria criteria){
        return new ResponseEntity<>(accountService.queryAll(criteria),HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<Object> create(@Validated @RequestBody Account resources){
        return new ResponseEntity<>(accountService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    public ResponseEntity<Object> update(@Validated @RequestBody Account resources){
        accountService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        accountService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/register")
    public ResponseEntity<Object> register(@RequestParam("jsonData") String jsonData){
        JSONObject jsonObj = JSONObject.parseObject(jsonData);
        Account resources = JSON.toJavaObject(jsonObj, Account.class);
        Account account = accountService.register(resources);
        return new ResponseEntity<>(account, HttpStatus.OK);
    }

    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestParam("jsonData") String jsonData){
        JSONObject jsonObj = JSONObject.parseObject(jsonData);
        Account account = accountService.login(jsonObj);
        return new ResponseEntity<>(account, HttpStatus.OK);
    }

    @PostMapping("/updateLastGameServer")
    public ResponseEntity<Object> updateLastGameServer(@RequestParam("jsonData") String jsonData){
        JSONObject jsonObj = JSONObject.parseObject(jsonData);
        Account resources = JSON.toJavaObject(jsonObj, Account.class);
        resources.setUpdatetime(Timestamp.valueOf(LocalDateTime.now()));
        accountService.update(resources);
        return new ResponseEntity<>(resources, HttpStatus.OK);
    }
}